﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Homework6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        
        private void label1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
                openFileDialog.Multiselect = false;
                openFileDialog.Title = "选择文本文件1";
                openFileDialog.Filter = "TXT|*.txt";
                string file = "";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    file = openFileDialog.FileName;
                }

                this.label1.Text = file;
            }
            catch (Exception exception)
            {
                this.label3.Text = "Error！";
                throw;
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
                openFileDialog.Multiselect = false;
                openFileDialog.Title = "选择文本文件2";
                openFileDialog.Filter = "TXT|*.txt";
                string file = "";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    file = openFileDialog.FileName;
                }

                this.label2.Text = file;
            }
            catch (Exception exception)
            {
                this.label3.Text = "Error！";
                throw;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string data = Directory.GetCurrentDirectory() + "\\Data";
                string file1 = this.label1.Text;
                string file2 = this.label2.Text;
                if (file1 == "" || file2 == "")
                {
                    this.label3.Text = "源文件缺失";
                    return;
                }
                if (!Directory.Exists(data)) Directory.CreateDirectory(data);//如果Data目录不存在则创建
                if (File.Exists(data + "\\3.txt")) File.Delete(data + "\\3.txt");//如果新文件存在则删除
                File.AppendAllText(data + "\\3.txt", File.ReadAllText(file1));
                File.AppendAllText(data + "\\3.txt", File.ReadAllText(file2));
                this.label3.Text = "合并成功！";
            }
            catch (Exception Exception)
            {
                this.label3.Text = "Error！";
                throw;
            }
        }

        private void label3_Click(object sender, EventArgs e){}
    }
}
